﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using PPAP.Model;
using System.Data;

namespace PPAP.Web.Part
{
    public partial class Approved : PageBase
    {
        PPARBO bo = new PPARBO();
        LogBO lbo = new LogBO();
        SamplecardBO sbo = new SamplecardBO();
        SetLevelBO stbo = new SetLevelBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = null;
                if (CurrentUser.UnitId == "0")
                    dt = bo.GetListForSupplierDecided2(CurrentUser.UnitName).Tables[0];
                else
                    dt = bo.GetListForSupplierDecided3(CurrentUser.Id).Tables[0];
                dgList.DataSource = dt;
                dgList.DataBind();
            }
        }

        protected override void SkinControl()
        {
            //base.SkinControl();
        }


        //状态显示
        public string ShowAuditStatus(string Status)
        {
            string result = "";
            //if (Status.ToLower() + IsSave + IsReturn == "false01")
            //{
            result = "<span style='color:red'>已提交</span>";

            //}
            //else
            //{
            switch (Status.ToLower())
            {
                case "0":
                    result = "<span style='color:blue'></span>";
                    break;
                case "1":
                    result = "<span style='color:red'>Created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>Submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>Not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>Circulating</span>";
                    break;
                case "6":
                    result = "<span style='color:blue'>Released</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>Decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            //}
            return result;
        }

        //状态显示
        public string ShowImage(string Status)
        {
            string result = "";
            result = "image/add.gif";

            switch (Status.ToLower())
            {
                case "0":
                    result = "image/add.gif";
                    break;
                case "1":
                    result = "image/edit.png";
                    break;
                case "2":
                    result = "image/show.gif";
                    break;
                case "3":
                    result = "image/show.gif";
                    break;
                case "4":
                    result = "image/show.gif";
                    break;
                case "5":
                    result = "image/show.gif";
                    break;
                case "6":
                    result = "image/show.gif";
                    break;
                case "7":
                    result = "image/show.gif";
                    break;

                default:
                    result = "image/show.gif";
                    break;
            }
            //}
            return result;
        }


        //状态显示
        public string ShowResultImage(string Status)
        {
            string result = "";
            result = "image/G.png";

            switch (Status.Trim())
            {
                case "G":
                    result = "image/G.png";
                    break;
                case "Y":
                    result = "image/Y.png";
                    break;
                case "R":
                    result = "image/R.png";
                    break;
             

                default:
                    result = "image/G.png";
                    break;
            }
            //}
            return result;
        }

        //状态显示
        public bool ShowButton(string Status)
        {
            bool result = false;

            switch (Status.Trim())
            {
                case "G":
                    result = true;
                    break;
                case "Y":
                    result = true;
                    break;
                case "R":
                    result = true;
                    break;
             

                default:
                    result = false;
                    break;
 
            }
            return result;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void Unnamed1_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton ib = sender as ImageButton;
            string url = "Report.aspx?rid=" + ib.CssClass;
            Page.RegisterStartupScript("AlertMessage", string.Format("<script>open('{0}','newwindow','height=580,width=400,top=0,left=150,toolbar=no,menubar=yes,scrollbars=no,resizable=no,location=no,status=no')</script>", url));
            //Page.RegisterStartupScript("AlertMessage", "<script language='javascript'>javascript:PopUpWindow(" + url + ", 400, 150, 580, 400);</script>");
	
            //AlertMessage(ib.CssClass);
        }

        /// <summary>
        /// Resubmit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImageButton1_Click(object sender, EventArgs e)
        {
            try
            {
            Button image= (Button)(sender);
            if (image != null)
            {
                PPAR p = bo.GetModel(int.Parse(image.ToolTip));
                if (p != null)
                {
                    p.FINALSTATUS = 9;  //置为已重新提交状态
                    int num = bo.UpdateForSupplier(p);
                    List<Samplecard> list = sbo.GetListForPPAR(p.ID.ToString());
                    List<SetLevel> listst = stbo.GetListForPid(p.ID.ToString());
                    if (num > 0)
                    {
                        p.PPARVERSION = GetPPARVERSION(p.PPARVERSION);
                        p.STATUS = 0;
                        p.FINALSTATUS = 0;
                        num = bo.Resubmit(p);
                        if (num > 0)
                        {
                            foreach (Samplecard sa in list)
                            {
                                sa.PPARID = num;
                                if (sa.STATUS == 3 && (sa.RESULT == "Y" || sa.RESULT == "R"||sa.RESULT=="G"))
                                {
                                    sa.Issendmail = 9;
                                }
                                sbo.ReAdd(sa);
                            }
                            foreach (SetLevel st in listst)
                            {
                                st.PPARID = num;
                                stbo.Add(st);
                            }
                            lbo.WriteLog(CurrentUser.Id, "Resubmit PPAR", image.ToolTip);
                            string url = "SupplierPPARWeb.aspx?RID=" + num.ToString();
                            //Response.Redirect(url,false);
                            Page.RegisterStartupScript("AlertRedirect", "<script language='javascript'>window.location.href='" + url + "';top.frames['menu'].location.reload();</script>");
                        }
                    }
                }
            }
            }
            catch (Exception ex)
            {
                AlertMessage(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 版本号加一
        /// </summary>
        /// <param name="MaxNo"></param>
        /// <returns></returns>
        private string GetPPARVERSION(string num)
        {
            int MaxNo = int.Parse(num) + 1;
            string NewNo = "";
            switch (MaxNo.ToString().Length)
            {
                case 1:
                    NewNo = "0" + MaxNo;
                    break;
                default:
                    NewNo = MaxNo.ToString();
                    break;
            }
            return NewNo;
        }


    }
}